// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//  »Project«   Talina Gaming System (TgS) (∂)
//  »File«      TgS Common - Math API [Polynomial].c_inc
//  »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
//  »Version«   4.0
// ------------------------------------------------------------------------------------------------------------------------------ //
//  Copyright: © 2002-2010, Andrew Aye.  All Rights Reserved.
//  This software is free for non-commercial use. Redistribution and use in source and binary forms, with or without modification,
//  are permitted provided that the following conditions are met: 
//    Redistributions of source code must retain this copyright notice, this list of conditions and the following disclaimers. 
//    Redistributions in binary form must reproduce this copyright notice, this list of conditions and the following
//      disclaimers in the documentation and other materials provided with the distribution. 
//  Neither the names of the copyright owner nor the names of its contributors may be used to endorse or promote products derived
//  from this software without specific prior written permission. 
//  The intellectual property rights of the algorithms used reside with Andrew Aye.  You may not use this software, in whole or
//  in part, in support of any commercial product without the express written consent of the author.
//  There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //

// ---- MATRIX ARITHMETIC OPERATIONS -------------------------------------------------------------------------------------------- //

TgVOID M(F_CAT_IMPL)( M(PCU_TgMAT) ptmRet, M(CPCU_TgMAT) ptmM0, M(CPCU_TgMAT) ptmM1 )
{
    TgUINT32                            ui0;

    for (ui0 = 0; ui0 < 4; ++ui0)
    {
        ptmRet->m_atyRowCol[ui0][0] =  ptmM0->m_atyRowCol[ui0][0] * ptmM1->m_atyRowCol[0][0]
                                    +  ptmM0->m_atyRowCol[ui0][1] * ptmM1->m_atyRowCol[1][0]
                                    +  ptmM0->m_atyRowCol[ui0][2] * ptmM1->m_atyRowCol[2][0]
                                    +  ptmM0->m_atyRowCol[ui0][3] * ptmM1->m_atyRowCol[3][0];

        ptmRet->m_atyRowCol[ui0][1] =  ptmM0->m_atyRowCol[ui0][0] * ptmM1->m_atyRowCol[0][1]
                                    +  ptmM0->m_atyRowCol[ui0][1] * ptmM1->m_atyRowCol[1][1]
                                    +  ptmM0->m_atyRowCol[ui0][2] * ptmM1->m_atyRowCol[2][1]
                                    +  ptmM0->m_atyRowCol[ui0][3] * ptmM1->m_atyRowCol[3][1];

        ptmRet->m_atyRowCol[ui0][2] =  ptmM0->m_atyRowCol[ui0][0] * ptmM1->m_atyRowCol[0][2]
                                    +  ptmM0->m_atyRowCol[ui0][1] * ptmM1->m_atyRowCol[1][2]
                                    +  ptmM0->m_atyRowCol[ui0][2] * ptmM1->m_atyRowCol[2][2]
                                    +  ptmM0->m_atyRowCol[ui0][3] * ptmM1->m_atyRowCol[3][2];

        ptmRet->m_atyRowCol[ui0][3] =  ptmM0->m_atyRowCol[ui0][0] * ptmM1->m_atyRowCol[0][3]
                                    +  ptmM0->m_atyRowCol[ui0][1] * ptmM1->m_atyRowCol[1][3]
                                    +  ptmM0->m_atyRowCol[ui0][2] * ptmM1->m_atyRowCol[2][3]
                                    +  ptmM0->m_atyRowCol[ui0][3] * ptmM1->m_atyRowCol[3][3];
    };
}




// ---- INVERSE FUNCTIONS ------------------------------------------------------------------------------------------------------- //

TgVOID M(F_INV_DET_IMPL)( M(PCU_TgMAT) ptmRet, const TYPE tyDet, M(CPCU_TgMAT) ptmM1 )
{
    if (F(tgCM_NR0)( tyDet ))
    {
        TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Matrix is Singular - Zero Matrix returned.\n"), TgT("Math"), TgT("F_INV") );
        M(F_CLR)( ptmRet );
    }
    else
    {
        C_TYPE                              tyInvDet = MKL(1.0) / tyDet;

        ptmRet->m.f11 =  tyInvDet*( ptmM1->m.f22*(ptmM1->m.f33*ptmM1->m.f44 - ptmM1->m.f34*ptmM1->m.f43)
                                  - ptmM1->m.f32*(ptmM1->m.f23*ptmM1->m.f44 - ptmM1->m.f24*ptmM1->m.f43)
                                  + ptmM1->m.f42*(ptmM1->m.f23*ptmM1->m.f34 - ptmM1->m.f24*ptmM1->m.f33) );

        ptmRet->m.f12 = -tyInvDet*( ptmM1->m.f12*(ptmM1->m.f33*ptmM1->m.f44 - ptmM1->m.f34*ptmM1->m.f43)
                                  - ptmM1->m.f32*(ptmM1->m.f13*ptmM1->m.f44 - ptmM1->m.f14*ptmM1->m.f43)
                                  + ptmM1->m.f42*(ptmM1->m.f13*ptmM1->m.f34 - ptmM1->m.f14*ptmM1->m.f33) );

        ptmRet->m.f13 =  tyInvDet*( ptmM1->m.f12*(ptmM1->m.f23*ptmM1->m.f44 - ptmM1->m.f24*ptmM1->m.f43)
                                  - ptmM1->m.f22*(ptmM1->m.f13*ptmM1->m.f44 - ptmM1->m.f14*ptmM1->m.f43)
                                  + ptmM1->m.f42*(ptmM1->m.f13*ptmM1->m.f24 - ptmM1->m.f14*ptmM1->m.f23) );

        ptmRet->m.f14 = -tyInvDet*( ptmM1->m.f12*(ptmM1->m.f23*ptmM1->m.f34 - ptmM1->m.f24*ptmM1->m.f33)
                                  - ptmM1->m.f22*(ptmM1->m.f13*ptmM1->m.f34 - ptmM1->m.f14*ptmM1->m.f33)
                                  + ptmM1->m.f32*(ptmM1->m.f13*ptmM1->m.f24 - ptmM1->m.f14*ptmM1->m.f23) );

        ptmRet->m.f21 = -tyInvDet*( ptmM1->m.f21*(ptmM1->m.f33*ptmM1->m.f44 - ptmM1->m.f34*ptmM1->m.f43)
                                  - ptmM1->m.f31*(ptmM1->m.f23*ptmM1->m.f44 - ptmM1->m.f24*ptmM1->m.f43)
                                  + ptmM1->m.f41*(ptmM1->m.f23*ptmM1->m.f34 - ptmM1->m.f24*ptmM1->m.f33) );

        ptmRet->m.f22 =  tyInvDet*( ptmM1->m.f11*(ptmM1->m.f33*ptmM1->m.f44 - ptmM1->m.f34*ptmM1->m.f43)
                                  - ptmM1->m.f31*(ptmM1->m.f13*ptmM1->m.f44 - ptmM1->m.f14*ptmM1->m.f43)
                                  + ptmM1->m.f41*(ptmM1->m.f13*ptmM1->m.f34 - ptmM1->m.f14*ptmM1->m.f33) );

        ptmRet->m.f23 = -tyInvDet*( ptmM1->m.f11*(ptmM1->m.f23*ptmM1->m.f44 - ptmM1->m.f24*ptmM1->m.f43)
                                  - ptmM1->m.f21*(ptmM1->m.f13*ptmM1->m.f44 - ptmM1->m.f14*ptmM1->m.f43)
                                  + ptmM1->m.f41*(ptmM1->m.f13*ptmM1->m.f24 - ptmM1->m.f14*ptmM1->m.f23) );

        ptmRet->m.f24 =  tyInvDet*( ptmM1->m.f11*(ptmM1->m.f23*ptmM1->m.f34 - ptmM1->m.f24*ptmM1->m.f33)
                                  - ptmM1->m.f21*(ptmM1->m.f13*ptmM1->m.f34 - ptmM1->m.f14*ptmM1->m.f33)
                                  + ptmM1->m.f31*(ptmM1->m.f13*ptmM1->m.f24 - ptmM1->m.f14*ptmM1->m.f23) );

        ptmRet->m.f31 =  tyInvDet*( ptmM1->m.f21*(ptmM1->m.f32*ptmM1->m.f44 - ptmM1->m.f34*ptmM1->m.f42)
                                  - ptmM1->m.f31*(ptmM1->m.f22*ptmM1->m.f44 - ptmM1->m.f24*ptmM1->m.f42)
                                  + ptmM1->m.f41*(ptmM1->m.f22*ptmM1->m.f34 - ptmM1->m.f24*ptmM1->m.f32) );

        ptmRet->m.f32 = -tyInvDet*( ptmM1->m.f11*(ptmM1->m.f32*ptmM1->m.f44 - ptmM1->m.f34*ptmM1->m.f42)
                                  - ptmM1->m.f31*(ptmM1->m.f12*ptmM1->m.f44 - ptmM1->m.f14*ptmM1->m.f42)
                                  + ptmM1->m.f41*(ptmM1->m.f12*ptmM1->m.f34 - ptmM1->m.f14*ptmM1->m.f32) );

        ptmRet->m.f33 =  tyInvDet*( ptmM1->m.f11*(ptmM1->m.f22*ptmM1->m.f44 - ptmM1->m.f24*ptmM1->m.f42)
                                  - ptmM1->m.f21*(ptmM1->m.f12*ptmM1->m.f44 - ptmM1->m.f14*ptmM1->m.f42)
                                  + ptmM1->m.f41*(ptmM1->m.f12*ptmM1->m.f24 - ptmM1->m.f14*ptmM1->m.f22) );

        ptmRet->m.f34 = -tyInvDet*( ptmM1->m.f11*(ptmM1->m.f22*ptmM1->m.f34 - ptmM1->m.f24*ptmM1->m.f32)
                                  - ptmM1->m.f21*(ptmM1->m.f12*ptmM1->m.f34 - ptmM1->m.f14*ptmM1->m.f32)
                                  + ptmM1->m.f31*(ptmM1->m.f12*ptmM1->m.f24 - ptmM1->m.f14*ptmM1->m.f22) );

        ptmRet->m.f41 = -tyInvDet*( ptmM1->m.f21*(ptmM1->m.f32*ptmM1->m.f43 - ptmM1->m.f33*ptmM1->m.f42)
                                  - ptmM1->m.f31*(ptmM1->m.f22*ptmM1->m.f43 - ptmM1->m.f23*ptmM1->m.f42)
                                  + ptmM1->m.f41*(ptmM1->m.f22*ptmM1->m.f33 - ptmM1->m.f23*ptmM1->m.f32) );

        ptmRet->m.f42 =  tyInvDet*( ptmM1->m.f11*(ptmM1->m.f32*ptmM1->m.f43 - ptmM1->m.f33*ptmM1->m.f42)
                                  - ptmM1->m.f31*(ptmM1->m.f12*ptmM1->m.f43 - ptmM1->m.f13*ptmM1->m.f42)
                                  + ptmM1->m.f41*(ptmM1->m.f12*ptmM1->m.f33 - ptmM1->m.f13*ptmM1->m.f32) );

        ptmRet->m.f43 = -tyInvDet*( ptmM1->m.f11*(ptmM1->m.f22*ptmM1->m.f43 - ptmM1->m.f23*ptmM1->m.f42)
                                  - ptmM1->m.f21*(ptmM1->m.f12*ptmM1->m.f43 - ptmM1->m.f13*ptmM1->m.f42)
                                  + ptmM1->m.f41*(ptmM1->m.f12*ptmM1->m.f23 - ptmM1->m.f13*ptmM1->m.f22) );

        ptmRet->m.f44 =  tyInvDet*( ptmM1->m.f11*(ptmM1->m.f22*ptmM1->m.f33 - ptmM1->m.f23*ptmM1->m.f32)
                                  - ptmM1->m.f21*(ptmM1->m.f12*ptmM1->m.f33 - ptmM1->m.f13*ptmM1->m.f32)
                                  + ptmM1->m.f31*(ptmM1->m.f12*ptmM1->m.f23 - ptmM1->m.f13*ptmM1->m.f22) );
    };
}